/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Xml;

namespace BlindShark.GameElements
{
	/// <summary>
	/// The static profile of a player, not linked to a game
	/// </summary>
	public class Profile
	{
		#region Variables
		private string _sName;
		private string _sPictureFile;
		private string _sDescription;
		#endregion
		
		#region Constructors
		/// <summary>
		/// An empty profile
		/// </summary>
		public Profile()
		{
			
		}
		
		/// <summary>
		/// Build a profile from a Xml Files
		/// </summary>
		/// <param name="filename">Source Filename</param>
		public Profile(string filename)
		{
			 ReadFromXml(filename);
		}
		#endregion
		
		#region Xml Functions
		/// <summary>
		/// Save the profile's informations into a Xml File
		/// </summary>
		/// <param name="saveFile">Filename to be saved</param>
		public void SaveToXml(string saveFile)
		{
			// A XmlTextWriter will be used to save informations
			XmlTextWriter xWriter = null;
			try
			{
				xWriter = new XmlTextWriter(saveFile, System.Text.Encoding.UTF8);
				xWriter.Formatting = Formatting.Indented;
				xWriter.WriteStartDocument(true);
				// Creating the 'playerProfile' node
				xWriter.WriteStartElement("playerProfile");
				// Name and PictureFile are attributes of this node
				xWriter.WriteAttributeString("name", _sName);
				xWriter.WriteAttributeString("pictureFile", _sPictureFile);
				// A child node contains the description
				xWriter.WriteStartElement("description");
				xWriter.WriteString(_sDescription);
				xWriter.WriteEndElement();
				// Closing the nodes
				xWriter.WriteEndElement();
				xWriter.WriteEndDocument();
				
				xWriter.Flush();
				xWriter.Close();
			}
				
			finally
			{
				if (xWriter != null)
					xWriter.Close();
			}
		}
		
		/// <summary>
		/// Load the profile's informations from a Xml File
		/// </summary>
		/// <param name="fileName">Name of the File to be loaded</param>
		public bool ReadFromXml(string fileName)
		{
			XmlTextReader xReader = new XmlTextReader(fileName);
			try
			{
				if (!xReader.ReadToFollowing("playerProfile"))
					return false;
				if (!xReader.MoveToFirstAttribute())
					return false;
				// Reading the name
				if (xReader.Name.Equals("name"))
					_sName = xReader.Value;
				else 
					return false;
				// Reading the pictureFile name
				if (!xReader.MoveToNextAttribute())
					return false;
				if (xReader.Name.Equals("pictureFile"))
					_sPictureFile = xReader.Value;
				else
					return false;
				// Reading the description
				if (!xReader.ReadToFollowing("description"))
					return false;
				_sDescription = xReader.ReadString();
				
				// Reading end
				xReader.Close();
			}
			catch(Exception)
			{
				return false;
			}
			return true;
		}
		
		#endregion
		
		#region Accessors
		
		/// <summary>
		/// Player's Name
		/// </summary>
		public string Name
		{
			get {return _sName;}
			set {_sName = value;}
		}
		
		/// <summary>
		/// Filename of the player avatar
		/// </summary>
		public string PictureFile
		{
			get {return _sPictureFile;}
			set {_sPictureFile = value;}
		}
		
		/// <summary>
		///  Player's description
		/// </summary>
		public string Description
		{
			get {return _sDescription;}
			set {_sDescription = value;}
		}
		
		#endregion
		
		public override string ToString()
		{
			return _sName;
		}
	}
	
	/// <summary>
	/// The association between a File and a Profile
	/// </summary>
	public struct ProfileFile : IComparable
	{
		public Profile profile;
		public string file;
		
		public override string ToString()
		{
			return profile.ToString();
		}
		
		public int CompareTo(object comparer)
		{
			ProfileFile other = (ProfileFile) comparer;
			return profile.Name.CompareTo(other.profile.Name);
		}
	}
}
